<template>
	<view class="root-wrap">
		<view class="form-item">
			<view class="label">
				<text class="title">提供发票类别</text>
				<text class="require">*</text>
			</view>
			<view class="input-wrap" @click="typeListShow = true">
				<u-input
					placeholder="提供发票类别"
					border="none"
					disabled
					v-model="userInfo.invoice_type_text"
					suffixIcon="arrow-down"
					fontSize="28rpx"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">纳税人类别</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap" @click="taxpayerListShow = true">
				<u-input
					placeholder="纳税人类别"
					border="none"
					disabled
					suffixIcon="arrow-down"
					v-model="userInfo.invoice_taxpayer_text"
					fontSize="28rpx"
				  ></u-input>
			</view>
		</view>
		<view class="submit"><u-button @click="onSubmit" color="#30D6C2" type="primary" shape="circle">保存<image class="icon" src="../../../static/assets/imgs/next-step.png"></image></u-button></view>
		<u-action-sheet :actions="typeList" :show="typeListShow" @select="typeListCallback"
					description="请选择发票类别" @close="typeListShow = false">
				</u-action-sheet>
		<u-action-sheet :actions="taxpayerList" :show="taxpayerListShow" @select="taxpayerListCallback"
					description="请选择纳税人类别" @close="taxpayerListShow = false">
				</u-action-sheet>
	</view>
</template>

<script>
	import { getUserInfo,updateInvoiceInfo,getMiscInfo } from '@/api/user.js'
	export default {
		data() {
			return {
				userInfo:{},
				typeList:[],
				taxpayerList:[],
				typeListShow: false,
				taxpayerListShow: false,
			};
		},
		onLoad(){
			this.userInfo = this.vuex_user;
			this.initSelect();
			this.getUserInfo();
		},
		methods:{
			initSelect(){
				getMiscInfo().then((res)=>{
					let obj = res.data.invoiceType;
					Object.keys(obj).forEach(key => {
					  this.typeList.push({
						  name: obj[key],
						  value: key
					  })
					});
					let obj1 = res.data.invoiceTaxpayer;
					Object.keys(obj1).forEach(key => {
					  this.taxpayerList.push({
						  name: obj1[key],
						  value: key
					  })
					});
				})
			},
			getUserInfo(){
				getUserInfo().then((res)=>{
					if (1 == res.code) {
						this.userInfo = res.data;
						this.userInfo.invoice_type_text = res.data.invoice_type==''?'':(res.data.invoice_type=='normal'?'增值税普通发票':(res.data.invoice_type=='other'?'其他发票':'增值税专用发票'))
						this.userInfo.invoice_taxpayer_text = res.data.invoice_taxpayer==''?'':(res.data.invoice_taxpayer=='normal'?'一般纳税人':(res.data.invoice_taxpayer=='other'?'其他':'小规模纳税人'))
						this.$u.vuex('vuex_user', res.data)
					} else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			typeListCallback(e){
				this.userInfo.invoice_type_text = e.name
				this.userInfo.invoice_type = e.value
			},
			taxpayerListCallback(e){
				this.userInfo.invoice_taxpayer_text = e.name
				this.userInfo.invoice_taxpayer = e.value
			},
			onSubmit(){
				if(this.loading) return;
				const { id,invoice_type,invoice_taxpayer } = this.userInfo;
				
				if(!invoice_type || !invoice_taxpayer){
					uni.showToast({
						title:'请检查所有必填项',
						icon:'none'
					})
					return;
				}
				this.loading = true;
				uni.showLoading({
					title: '提交中...'
				})
				const params = {
					"vendor_id": id, //供应商ID
					"invoice_type": invoice_type, //发票类别
					"invoice_taxpayer": invoice_taxpayer //纳税人类别
				}
				updateInvoiceInfo(params).then(res => {
					this.loading = false;
					uni.hideLoading();
					if(res.code==1){
						uni.showToast({
							title:'提交成功',
							icon:'success'
						})
						const timer = setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
							clearTimeout(timer);
						},900);
					}else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.root-wrap{
	padding-bottom: 110rpx;
	.form-item{
		margin: 0 24rpx;
		.label{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			margin: 44rpx 0 24rpx;
			.title{
				font-size: 28rpx;
				color: #606266;
				line-height: 50rpx;
			}
			.require{
				font-size: 28rpx;
				color: #E62020;
				line-height: 50rpx;
				margin-left: 24rpx;
			}
		}
		.input-wrap{
			width: 700rpx;
			height: 90rpx;
			background-color: #F3F4F6;
			margin: 0 auto;
			border-radius: 8rpx;
			.u-input{
				height: 100% !important;
				input{
					padding: 0 24rpx !important;
				}
			}
		}
	}
	.submit{
		width: 208rpx;
		height: 80rpx;
		margin: 88rpx auto 110rpx;
		.icon{
			width: 48rpx;
			height: 48rpx;
			margin-left: 16rpx;
		}
		.label{
			font-weight: 500;
			font-size: 32rpx;
			color: #fff;
		}
	}
	.u-input__content__subfix-icon{
		margin-right: 16rpx;
	}
}
</style>
